package src.Packets;

import java.io.IOException;
import src.Util.Stream;
import src.Players.*;
import src.Main;

public class HandlePacket
{
    public void handlePacket(Player p)
    {
        if(p == null)
        {
            return;
        }
        switch(p.packetIndex)
        {
            case 100: //To fix, recieving this non-existant packet with each login.
            break;
            case 185: //All the ingame buttons.
                ActionButtons a = new ActionButtons(p);
                a = null;
            break;
            case 164: //Player walking.
            case 248: //Minimap walking.
            case 98: //Other movement, such as ground item pickup.
                Walk w = new Walk(p, p.packetSize);
                w = null;
            break;
            case 241: //Mouse clicks.
                p.inStream.readDWord();
            break;
            case 86: //Camera rotation.
                cameraCoordsUpdate(p);
            break;
            case 202: //Idle packet.
            break;
            case 4: //Regular player chat.
                regularChat(p);
            break;
            case 121: //Called everytime the player updates?
            break;
            case 3: //If the client is placed behind/infront of another window, this integer changes.
            /*To do: temp ban players who send multiple packets if they are using an autoer(silent mouse kind)*/
                p.inStream.readUnsignedByte();
            break;
            case 103: // ::commands
                Commands c = new Commands(p);
                c = null;
            break;
            case 41: //Equiping items.
                Equipment e = new Equipment(p);
                e = null;
            break;
            case 145: //item option 1.
                ItemOption1 io1 = new ItemOption1(p);
                io1 = null;
            break;
            case 117: //item option 2.
                ItemOption2 io2 = new ItemOption2(p);
                io2 = null;
            break;
            case 43: //item option 3.
                ItemOption3 io3 = new ItemOption3(p);
                io3 = null;
            break;
            case 129: //item option 4.
                ItemOption4 io4 = new ItemOption4(p);
                io4 = null;
            break;
            case 132: //First object option.
                ObjectClick1 ob1 = new ObjectClick1(p);
                ob1 = null;
            break;
            case 252: //Second object option.
                ObjectClick2 ob2 = new ObjectClick2(p);
                ob2 = null;
            break;
            case 70: //Third object option.
                ObjectClick3 ob3 = new ObjectClick3(p);
                ob3 = null;
            break;
            case 210: //"Loading - Please Wait" packet.
                p.loadingDelay = 1;
            break;
            case 87: //Player item dropping.
                DropItem di = new DropItem(p);
                di = null;
            break;
            case 236: //Player item pickuping.
                PickupItem pi = new PickupItem(p);
                pi = null;
            break;
            case 214: //Switch two items in your inventory, bank, etc.
                SwitchItems si = new SwitchItems(p);
                si = null;
            break;
            case 40: //Dialogue continue, etc options.
                Dialogue d = new Dialogue(p);
                d = null;
            break;
            case 155: //NPC option select 1.
                NPCOption1 no1 = new NPCOption1(p);
                no1 = null;
            break;
            case 17: //NPC option select 2.
                NPCOption2 no2 = new NPCOption2(p);
                no2 = null;
            break;
            case 72: //Attack option on NPCs.
                attackNPC(p);
            break;
            case 122: //Item select, such as eating, drinking, etc.
                ItemSelect is = new ItemSelect(p);
                is = null;
            break;
            case 128: //Attack option on players.
                attackPlayer(p);
            break;
            case 249: //Spell casted on a player.
                MagicOnPlayer mop = new MagicOnPlayer(p);
                mop = null;
            break;
	    case 153: // trade with player
		tradePlayer(p);
            break;
	    case 53: // use item on item
		ItemOnItem ioi = new ItemOnItem(p);
		ioi = null;
	    break;
	    case 73: // was follow, using for duel
		duelPlayer(p);
	    break;
	    case 130:
		closeWindows(p);
	    break;
            case 226:
            case 78:
            case 148:
            case 183:
            case 230:
            case 136:
            case 189:
            case 152:
            case 200:
            case 85:
            case 165:
            case 238:
            case 150:
            case 36:
            case 246:
            case 77: //These packets when used properly can lower the amount of usable "cheat" clients.
            break;
            default:
                Main.m.log("[" + p.playerIndex + "-" + p.username + "] Unhandled packet " + p.packetIndex);
            break;
        }
    }

    private void closeWindows(Player p) 
    {
	if(p.inTrade == true) {
		Player plr = Main.m.ge.players[p.tradePlayer];
		plr.frame.sendMessage(plr, p.username+" declined the trade.");
		p.inTrade = false;
		plr.inTrade = false;
		p.frame.removeAllWindows(p);
		plr.frame.removeAllWindows(plr);
	} else if(p.inDuel == true) {
		Player plr = Main.m.ge.players[p.duelPlayer];
		plr.frame.sendMessage(plr, p.username+" declined the duel.");
		p.inDuel = false;
		plr.inDuel = false;
		p.frame.removeAllWindows(p);
		plr.frame.removeAllWindows(plr);
	}
    }

    private void attackNPC(Player p)
    {
        if(p == null)
        {
            return;
        }
        p.attackNPC = p.inStream.readUnsignedWordA();
        p.attackingNPC = true;
        PlayerAttack pl = new PlayerAttack(p);
        pl.attackNPC();
        pl = null;
    }

    private void attackPlayer(Player p)
    {
        if(p == null)
        {
            return;
        }
        p.attackPlayer = p.inStream.readUnsignedWord();
        p.attackingPlayer = true;
        PlayerAttack pl = new PlayerAttack(p);
        pl.attackPlayer();
        pl = null;
    }

    private void tradePlayer(Player p)
    {
        if(p == null)
        {
            return;
        }
        p.tradePlayer = p.inStream.readUnsignedByte();
        p.tradingPlayer = true;
        PlayerTrade pt = new PlayerTrade(p);
        pt.tradePlayer();
	pt = null;
    }

    private void duelPlayer(Player p)
    {
        if(p == null)
        {
            return;
        }
        p.duelPlayer = p.inStream.readUnsignedByte();
        p.duelingPlayer = true;
       	PlayerDuel pd = new PlayerDuel(p);
        pd.duelPlayer();
	pd = null;
    }

    private void regularChat(Player p)
    {
        if(p == null)
        {
            return;
        }
        p.chatTextEffects = p.inStream.readUnsignedByteS();
        p.chatTextColor = p.inStream.readUnsignedByteS();
        p.chatTextSize = (byte)(p.packetSize - 2);
        p.inStream.readBytes_reverseA(p.chatText, p.chatTextSize, 0);
        p.chatTextUpdateRequired = true;
    }

    private void cameraCoordsUpdate(Player p)
    {
        if(p == null)
        {
            return;
        }
        p.inStream.readUnsignedWord();
        p.inStream.readUnsignedWordA();
    }

    public boolean login(Player p)
    {
        if(p == null)
        {
            return false;
        }
        PlayerLogin l = new PlayerLogin();
        boolean success = l.login(p);
        l = null;
        return success;
    }
}